package com.guanlixitong.controller.impl;

import com.guanlixitong.controller.IStuedntController;
import com.guanlixitong.domain.Student;
import com.guanlixitong.factory.Factory;
import com.guanlixitong.service.IStudentService;
import java.io.IOException;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImpl implements IStuedntController {

    private Scanner sc = new Scanner(System.in);

    private IStudentService studentService = Factory.getStudentService();

    //开始
    @Override
    public void start() throws Exception {
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("----欢迎来到学生信息管理系统----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.统计学生信息");
            System.out.println("6.查看日志");
            System.out.println("7.退出");
            System.out.println("请输入选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addstudent();
                    break;
                case "2":
                    System.out.println("删除学生");
                    deletestudent();
                    break;
                case "3":
                    System.out.println("修改学生");
                    updatestudent();
                    break;
                case "4":
                    chakan(sc);
                    break;
                case "5":
                    tongji();
                    break;
                case "6":
                    System.out.println("查看日志");
                    readRizhi();
                    break;
                case "7":
                    studentService.tst();
                    break loop;
            }
        }
    }

    //统计选项
    public void tongji() {
        while (true) {
            System.out.println(" 1.男女生各有多少?   2.  有2个的微信号的人个数  3.00后的人数  4.手机号中至少包含2个6的 \n" +
                    " 5. 某个姓的同学有多少人？  6. 名字长度是3的学生有多少人？  7.班级人数   8.18,19,20三个年龄段的学生有多少人?\n"+
                    " 9.不同年龄段的人数  10. 各组平均年龄  11.各组人数  12.各个姓的人数 13.各个科目的人数  100.退出  ");
            String choice2 = sc.next();
            if ("100".equals(choice2))return;
            studentService.Tongji(choice2);
            System.out.println("----------------------------------------------------");
        }
    }

    //查看信息主界面
    private void chakan(Scanner sc) throws Exception {
        loop:
        while (true) {
            System.out.println("1.按姓名查看学生   2.按手机号查看学生   3.查看全部信息  4.导入Excel表  5.导出Excel表  6.返回 ");
            String choice1 = sc.next();
            switch (choice1) {
                case "1":
                    findname();
                    break;
                case "2":
                    findPhone();
                    break;
                case "3":
                    findAllstudent();
                    break;
                case "4":
                    importExcel();
                    break;
                case "5":
                    excel();
                    break;
                case "6":
                    System.out.println("返回学生管理系统界面");
                    break loop;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }
    }

    //增加
    @Override
    public void addstudent() throws Exception {
        String id;
        while (true) {
            System.out.println("请输入学号：");
            id = sc.next();
            boolean exits = studentService.isExists(id);
            if (!exits) {
                break;
            } else {
                System.out.println("学号已存在");
            }
        }
        Student student = getStudentInfo(id);
        studentService.addstudent(student);
        System.out.println("添加成功");


    }

    //输入学生信息
    public Student getStudentInfo(String id) {
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入性别：");
        String sex = sc.next();
        System.out.println("请输入组号：");
        int group = sc.nextInt();
        System.out.println("请输入科目： ");
        String subject = sc.next();
        System.out.println("请输入电话：");
        String phone = sc.next();
        System.out.println("请问有几个微信号：");
        int i = sc.nextInt();
        ArrayList<String> lists;
        if (i == 1) {
            System.out.println("请输入微信号：");
            String list = sc.next();
            lists = new ArrayList<>();
            lists.add(list);
        } else {
            System.out.println("请输入微信号1：");
            String list = sc.next();
            System.out.println("请输入微信号2：");
            String list2 = sc.next();
            lists = new ArrayList<>();
            lists.add(list);
            lists.add(list2);
        }
        int day = 0;
        int year = 0;
        int month = 0;
        ll:
        while (true) {
            System.out.println("请输入生日年份：");
            year = sc.nextInt();
            if (year >= 1000 && year <= 2022) {
                System.out.println("请输入生日月份(1-12)：");
                month = sc.nextInt();
                for (int k = 0; k <= 12; k++) {
                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                        System.out.println("请输入生日天数：");
                        day = sc.nextInt();
                        if (day > 31 || day <= 0) {
                            System.out.println("该月没有此天，请重新输入");
                            break;
                        } else {
                            break ll;
                        }
                    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                        System.out.println("请输入生日天数：");
                        day = sc.nextInt();
                        if (day > 30 || day <= 0) {
                            System.out.println("该月没有此天，请重新输入");
                            break;
                        } else {
                            break ll;
                        }
                    } else if (month == 2) {
                        System.out.println("请输入生日天数：");
                        day = sc.nextInt();
                        if (year % 4 == 0) {
                            if (day < 0 || day > 29) {
                                System.out.println("该年为闰年，2月最多有29天，请重新输入");
                                break;
                            } else {
                                break ll;
                            }
                        } else {
                            if (day > 28 || day < 0) {
                                System.out.println("该年为平年，2月最多有28天，请重新输入");
                                break;
                            } else {
                                break ll;
                            }
                        }
                    } else if (month <= 0 || month > 12) {
                        System.out.println("输入错误，请重新输入");
                        break;
                    }
                }

            } else {
                System.out.println("请重新输入");
            }
        }
        LocalDate birthday2 = LocalDate.of(year, month, day);
        LocalDate now = LocalDate.now();
        Period between = Period.between(birthday2, now);
        long l = between.toTotalMonths();
        int age = (int) l / 12;
        return new Student(id, name, sex, age, birthday2, group, subject, phone, lists);
    }

    //删除
    @Override
    public void deletestudent() throws Exception {
        if (isNull()) return;

        String sid;
        System.out.println("请输入学号：");
        while (true) {
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {

                break;
            } else {
                System.out.println("该学号不存在，请重新输入");
            }
        }
        System.out.println("删除成功");
        studentService.deletestudent(sid);
    }

    //修改
    @Override
    public void updatestudent() throws Exception {
        if (isNull()) return;
        String id;
        while (true) {
            System.out.println("请输入学号：");
            id = sc.next();
            boolean exits = studentService.isExists(id);
            if (!exits) {
                System.out.println("学号不存在");
            } else {
                break;
            }
        }
        Student student = getStudentInfo(id);
        studentService.updatestudent(id, student);
        System.out.println("修改成功");
    }

    //姓名查找信息
    @Override
    public void findname() throws Exception {
        ArrayList<Student> list = studentService.findAllstudent();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据，请先添加学生");
            return;
        }

        System.out.println("请输入姓名");
        String ad = sc.next().trim();
        int i = 0;
        System.out.println("学号\t姓名\t性别\t年龄\t生日\t组号\t科目\t电话\t微信号");
        while (i<list.size()) {
            boolean flag = false;
            while (i < list.size()) {
                i++;
                String name = list.get(i-1).getName();
                if (name.contains(ad)) {
                    flag = true;
                    break;
                }
            }
            if (flag){
                System.out.println(list.get(i-1).getId() + "\t" + list.get(i-1).getName() + "\t" + list.get(i-1).getSex()
                        + "\t" + list.get(i-1).getAge() + "\t" + list.get(i-1).getBirthday() + "\t" + list.get(i-1).getGroup()
                        + "\t"+list.get(i-1).getSubject()+"\t" + list.get(i-1).getPhone() + "\t" + list.get(i-1).getList());
            }
        }
        System.out.println("--------------");

    }

    //电话查找信息
    @Override
    public void findPhone() throws Exception {
        ArrayList<Student> list = studentService.findAllstudent();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据，请先添加学生");
            return;
        }
       /* boolean flag = false;
        System.out.println("请输入电话");
        String ad = sc.next().trim();
        int i =0;
        for (; i < list.size(); i++) {
            if (list.get(i).getPhone().equals(ad)) {
                flag = true;
                break;
            }
        }
        if (flag){
            System.out.println("学号\t姓名\t性别\t年龄\t生日\t组号\t科目\t电话\t微信号");
            System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getSex()
                    + "\t" + list.get(i).getAge() + "\t" + list.get(i).getBirthday() + "\t" + list.get(i).getGroup()
                    + "\t"+list.get(i).getSubject()+"\t" + list.get(i).getPhone() + "\t" + list.get(i).getList());
        }else {
            System.out.println("查看失败");
        }*/
        System.out.println("请输入电话");
        String ad = sc.next().trim();
        int i = 0;
        System.out.println("学号\t姓名\t性别\t年龄\t生日\t组号\t科目\t电话\t微信号");
        while (i<list.size()) {
            boolean flag = false;
            for (; i < list.size(); ) {
                i++;
                String phone = list.get(i - 1).getPhone();
                if (phone.contains(ad)) {
                    flag = true;
                    break;
                }
            }
            if (flag){
                System.out.println(list.get(i-1).getId() + "\t" + list.get(i-1).getName() + "\t" + list.get(i-1).getSex()
                        + "\t" + list.get(i-1).getAge() + "\t" + list.get(i-1).getBirthday() + "\t" + list.get(i-1).getGroup()
                        + "\t"+list.get(i-1).getSubject()+"\t" + list.get(i-1).getPhone() + "\t" + list.get(i-1).getList());
            }
        }
        System.out.println("--------------");
    }

    //导入表格
    @Override
    public void importExcel() throws Exception {
        System.out.println("请输入文件的绝对路径");
        String s = sc.next();
        studentService.importExcel(s);
    }

    //查看全部学生信息
    @Override
    public void findAllstudent() throws Exception {
        ArrayList<Student> list = studentService.findAllstudent();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据，请输入");
            return;
        }
        System.out.println("学号\t姓名\t性别\t年龄\t生日\t组号\t科目\t电话\t微信号"); //id, name, sex, age, birthday2, group, phone, lists
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + "\t" + list.get(i).getName() + "\t" + list.get(i).getSex()
                    + "\t" + list.get(i).getAge() + "\t" + list.get(i).getBirthday() + "\t" + list.get(i).getGroup()
                    + "\t" + list.get(i).getSubject() + "\t" + list.get(i).getPhone() + "\t" + list.get(i).getList());
        }
         System.out.println("查看成功");
    }

    //判断是否为空
    public final boolean isNull() throws Exception {
        ArrayList<Student> students = studentService.findAllstudent();
        if (students == null) {
            System.out.println("表中无人，请先添加数据");
            return true;
        }
        return false;
    }

    //查看日志
    @Override
    public void readRizhi() throws Exception {
        studentService.readRizhi();
    }

    //导出
    @Override
    public void excel() throws Exception {
        studentService.excel();
    }

    //退出
    @Override
    public void exit() throws IOException {
        studentService.exit();
    }

}



